package javatest.collections;

import java.util.ArrayList;
import java.util.Stack;

public class StackHelper{
    private Stack<String> stack = new Stack<>();
    public StackHelper(ArrayList<String> arrayList) {
        this.stack.addAll(arrayList);
    }
    public void reverse(){
        for(int i=0; i<stack.size(); i++){
            moveLast(i);
        }
    }
    private void moveLast(int index){
        if(stack.size() == index +1) return;
         
        String last = stack.pop();
        String prev = stack.pop();
        stack.add(last);
        moveLast(index);
        stack.add(prev);
        
    }

    public void reverse2(Stack<String> stack){
        if (stack.isEmpty()){
            return;
        }
        String i = f(stack);
        reverse2(stack);
        stack.push(i);
    }
    public String f(Stack<String> stack){
        String result = stack.pop();
        if(stack.isEmpty()){
            return result;
        }else{
            String last = f(stack);
            stack.push(result);
            return last;
        }

    }


    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        ArrayList<String> l = new ArrayList<String>();
        for(int i=0; i<2200; i++){
            l.add(""+i);
        }
        StackHelper s = new StackHelper(l);
        // s.reverse();
        s.reverse2(s.stack);
        // while(!s.stack.isEmpty()){
        //     System.out.print(s.stack.pop());
        // }
        long end = System.currentTimeMillis();
        System.out.println("use time: " + (end - start) + " ms");
    }
}
